﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ePioneer_DatePicker : System.Web.UI.UserControl
{
    private string _stringValue = string.Empty;
    private DateTime _datetimeValue = new DateTime();

    /// <summary>
    /// 返回 String 类型时间
    /// </summary>
    public string StringValue
    {
        get { return TextBoxTime.Text.Trim(); }
        set { _stringValue = value; }
    }

    /// <summary>
    /// 返回 DateTime 类型时间
    /// </summary>
    public DateTime DateTimeValue
    {
        get
        {
            if (TextBoxTime.Text.Trim() != "")
            {
                return Convert.ToDateTime(TextBoxTime.Text.Trim());
            }
            else
            {
                return Convert.ToDateTime("1900-01-01");
            }
        }
        set
        {
            _datetimeValue = value;
            TextBoxTime.Text = _datetimeValue.ToString("yyyy-MM-dd").Replace(@"/", @"-");

        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //如果有给值，则赋值给文本框
            if (_stringValue.Trim() != "")
            {
                TextBoxTime.Text = _stringValue.Trim();
            }
            if ((_datetimeValue.Year == 1 || _datetimeValue.Year == 1900) && (_datetimeValue.Month == 1) && (_datetimeValue.Day == 1))
            {
                TextBoxTime.Text = "";
            }
            else
            {
                TextBoxTime.Text = _datetimeValue.ToString("yyyy-MM-dd").Replace(@"/", @"-");
            }
        }
    }
    public void SetWidth(int width)
    {
        TextBoxTime.Width = width;
    }

    /// <summary>
    /// 清空内容
    /// </summary>
    public void Clear()
    {
        TextBoxTime.Text = "";
    }

    /// <summary>
    /// 注册控件的JavaScript
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPreRender(EventArgs e)
    {

        Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "WdatePicker", "../My97DatePicker/WdatePicker.js");
    }

    /// <summary>
    /// To check if the value in the textbox is a valid date.
    /// </summary>
    /// <param name="e"></param>
    public bool IsValid()
    {
        DateTime dateValue;
        return DateTime.TryParse(StringValue, out dateValue);
    }
    public override void Focus()
    {
        base.Focus();
        TextBoxTime.Focus();

    }
}